package com.sfpay.coreplatform.order.service.composite.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.account.common.enums.ExpenseFlag;
import com.sfpay.coreplatform.account.common.enums.TallyType;
import com.sfpay.coreplatform.account.valueobject.dto.Transfer;
import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.common.enums.PayStatus;
import com.sfpay.coreplatform.order.common.enums.tradeStatus;
import com.sfpay.coreplatform.order.service.composite.IAsyncHandle;
import com.sfpay.coreplatform.order.service.composite.common.PayContext;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException;
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.dto.Response;

@Service
public class AccountHandler extends AbstractHandler {

	private final static Logger logger = LoggerFactory
			.getLogger(AccountHandler.class);

	@Resource
	private IAsyncHandle asynService;

	@Override
	public Response Handler(PayRequest request, PayContext payContext) {		 
		Response response = new Response();
		Transfer transfer = new Transfer();
		transfer.setAccountPasswd(request.getPayerPassword());
		transfer.setAmount(request.getPayAmt());
		transfer.setBusinessSN(request.getOutTradeNo());
		transfer.setExpenseFlag(ExpenseFlag.PAYMENT);
		transfer.setMemberNo(request.getPayerMemberNo());
		transfer.setOrderNo(request.getTradePostNo());
		transfer.setPayeeAccount(request.getPayeeAcctNo());
		transfer.setPayerAccount(request.getPayerAcctNo());
		transfer.setPaymentNo(request.getPayPostNo());
		transfer.setPaymentOrderNo(payContext.getPayNo());
		transfer.setTallyType(TallyType.AUTO);
		transfer.setTransOrderNo(payContext.getTradeNo());
		transfer.setTallySource("OTHER");
		try {
			if (request.isAccPasword()) {// 记账要密码
				this.getAccountService().transfer(transfer);
			} else {
				this.getAccountService().transferWithoutPsw(transfer);
			}

			payContext.setAccCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);

		} catch (OrderServiceException e) {

			// 超时异常
			if (OrderResponseCode.HESSIAN_INVOKE_TIMEOUT == e.getCode()) {
				logger.error(
						"method:accountHandle tradeNo:"
								+ payContext.getTradeNo()
								+ " acc overtime exception ", e);
				payContext
						.setAccCode(OrderResponseCode.ORDER_RESPONSE_ACC_HANDEL_OVERTIME_FAIL);
				payContext.setAccMsg("method:accountHandle tradeNo:"
						+ payContext.getTradeNo() + " acc overtime exception");
				asynService.cancelAccHandel(request, payContext.getTradeNo(),
						payContext.getPayNo());// 异步处理销账
			} else {
				logger.error(
						"method:accountHandle tradeNo:"
								+ payContext.getTradeNo()
								+ " acc handel exception ", e);
				payContext
						.setAccCode(e.getCode());
				payContext.setAccMsg(e.getMessage());
			}
		}  

		response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);		 
		return response;
	}

	@Override
	public String getHandlerName() {
		 
		return "AccountHandler";
	}

}
